import wx
import ClsGen
import InterGen
import ProjGen

g_SrcDir = "../../src"

#-------------------------------------------------------------------------------
def MsgBox(parent, msg):

	dlg = wx.MessageDialog(
		parent,
		msg,
		"Code Generator",
		wx.OK | wx.ICON_INFORMATION)

	dlg.ShowModal()

#end def

#-------------------------------------------------------------------------------
def ErrorBox(parent, msg):
	dlg = wx.MessageDialog(
		parent,
		msg,
		"Code Generator",
		wx.OK | wx.ICON_STOP)

	dlg.ShowModal()

#end def

#-------------------------------------------------------------------------------
def ValidatePath(path):
	str = path;
	str.replace(' ', '');

	if(str == ''):
		return False

	return True

#end def

#-------------------------------------------------------------------------------
class ClsGenPanel(wx.Panel):
	def __init__(self, parent):
		wx.Panel.__init__(self, parent, -1)

		wx.StaticText(
			self,
			-1,
			"Class Name ( use / for sub-folder, e.g. core/gfx/render/renderer )",
			(12, 12),
			(360, 20))

		self.m_ClassNameTC = wx.TextCtrl(
			self,
			-1,
			"",
			(12, 36),
			(320, 20))

		self.m_GenCppCB = wx.CheckBox(
			self,
			-1,
			"Generate Cpp",
			(12, 64))

		self.m_GenCppCB.SetValue(True)

		self.m_GenerateBtn = wx.Button(self, 1, "&Generate", (296, 216), (80, 24))
		self.Bind(wx.EVT_BUTTON, self.OnClick, self.m_GenerateBtn)

	#end def

	def OnClick(self, event):
		if( ValidatePath(self.m_ClassNameTC.GetValue()) ):
			ClsGen.generate_class(
				g_SrcDir, 
				self.m_ClassNameTC.GetValue(),
				self.m_GenCppCB.GetValue() )
			
			MsgBox(
				self,
				'Class "' + self.m_ClassNameTC.GetValue() + ' was successfully generated!')

		else:
			ErrorBox(self, "Invalid class path!")

		#end if
	#end def

#end class

#-------------------------------------------------------------------------------
class InterGenPanel(wx.Panel):
	def __init__(self, parent):
		wx.Panel.__init__(self, parent, -1)

		wx.StaticText(
			self,
			-1,
			"Interface Name ( use / for sub-folder, e.g. core/gfx/render/renderer )",
			(12, 12),
			(360, 20))

		self.m_ClassNameTC = wx.TextCtrl(
			self,
			-1,
			"",
			(12, 36),
			(320, 20))

		self.m_GenerateBtn = wx.Button(self, 1, "&Generate", (296, 216), (80, 24))
		self.Bind(wx.EVT_BUTTON, self.OnClick, self.m_GenerateBtn)

	#end def

	def OnClick(self, event):
		if( ValidatePath( self.m_ClassNameTC.GetValue() ) ):
			InterGen.generate_interface(
				g_SrcDir,
				self.m_ClassNameTC.GetValue() )
			
			MsgBox(
				self,
				'Interface "' + self.m_ClassNameTC.GetValue() + 
				' was successfully generated!')

		else:
			ErrorBox(self, "Invalid interface path!")

		#end if
	#end def

#end class

#-------------------------------------------------------------------------------
class ProjGenPanel(wx.Panel):
	C_PROJECT_TYPES = ['Lib','Exe','Tests']
	C_COMPILER_TYPES = ['vc71', 'vc8']

	def __init__(self, parent):
		wx.Panel.__init__(self, parent, -1)

		wx.StaticText(
			self,
			-1,
			"Project Name ( use / for sub-folder, e.g. core/gfx/render )",
			(12, 12),
			(360, 20))

		self.m_ProjectNameTC = wx.TextCtrl(
			self,
			-1,
			"",
			(12, 36),
			(320, 20))

		self.m_ProjectTypeRB = wx.RadioBox(
			self,
			-1,
			"Project Type",
			(12,64),
			(120, 100),
			self.C_PROJECT_TYPES,
			1,
			wx.RA_SPECIFY_COLS)
			
		self.m_CompilerTypeRB = wx.RadioBox(
			self,
			-1,
			"Compiler Type",
			(140,64),
			(120, 100),
			self.C_COMPILER_TYPES,
			1,
			wx.RA_SPECIFY_COLS)

		self.m_GenerateBtn = wx.Button(self, 1, "&Generate", (296, 216), (80, 24))
		self.Bind(wx.EVT_BUTTON, self.OnClick, self.m_GenerateBtn)

	#end def

	def OnClick(self, event):

		if( ValidatePath(self.m_ProjectNameTC.GetValue()) ):
			ProjGen.generate_proj(
				g_SrcDir,
				self.m_ProjectNameTC.GetValue(),
				self.m_ProjectTypeRB.GetStringSelection(),
				"_" + self.m_CompilerTypeRB.GetStringSelection() )

			MsgBox(
				self,
				'Project "' + self.m_ProjectNameTC.GetValue() + '" was successfully generated!')
		else:
			ErrorBox(self, "Invalid project path!")

		#end if
	#end def

#end class

#-------------------------------------------------------------------------------
class SettingsPanel(wx.Panel):
	
	def __init__(self, parent):
		wx.Panel.__init__(self, parent, -1)

		wx.StaticText(
			self,
			-1,
			"Source Directory",
			(12, 12),
			(360, 20))

		self.m_SrcDirTxtCtrl = wx.TextCtrl(
			self,
			-1,
			g_SrcDir,
			(12, 36),
			(320, 20))
			
		self.m_SrcDirTxtCtrl.Bind(wx.EVT_TEXT, self.OnSrcDirChanged)
		
	#end def
	
	def OnSrcDirChanged(self, event):
		g_SrcDir = event.GetString()
		print "g_SrcDir = \"" + g_SrcDir + "\""
	#end def
	
#end class


#-------------------------------------------------------------------------------

class MainFrame(wx.Frame):
	C_FRAME_STYLE = (
		wx.SYSTEM_MENU +
		wx.CAPTION +
		wx.MINIMIZE_BOX +
		wx.CLOSE_BOX +
		wx.CLIP_CHILDREN)

	def __init__(self, parent):
		wx.Frame.__init__(
			self,
			parent,
			-1,
			"Code Generator",
			size=(400, 300),
			style=self.C_FRAME_STYLE)

		font = wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, "Microsoft Sans Serif")
		self.SetFont(font)

		self.m_Notebook = wx.Notebook(self)
		self.m_Notebook.AddPage(ProjGenPanel(self.m_Notebook), "Project")
		self.m_Notebook.AddPage(ClsGenPanel(self.m_Notebook), "Class")
		self.m_Notebook.AddPage(InterGenPanel(self.m_Notebook), "Interface")
		self.m_Notebook.AddPage(SettingsPanel(self.m_Notebook), "Settings")
		
	#end def

#end class

#-------------------------------------------------------------------------------
class CodeGenApp(wx.App):
	def OnInit(self):
		m_Frame = MainFrame(None)
		self.SetTopWindow(m_Frame)
		m_Frame.Show(True)

		return True
	#end def

#end class

#-------------------------------------------------------------------------------
app = CodeGenApp(redirect=False)
app.MainLoop()

#-------------------------------------------------------------------------------